/*
 *  This Activity is used for fetching category names of a particular user , creates category if required
 *  and can save text note on server and even share the text note on Facebook.
 */
package com.aoasaa;
  
import com.sendingFBpost.FBAppActivity;
import com.userrequestdtos.CreateCategoryDTO;
import com.userrequestdtos.FileInfoStoringDTO;
import com.userrequestdtos.ViewingCategoryAndTitleDTO;
import com.userutils.WSConnectionFile;
import com.userutils.WSConnectionStrings;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface; 
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class CreatingTextNotes extends Activity implements OnItemSelectedListener{
	EditText title,contents;
	static String[] categorylist={"h","k"};
	Spinner sp;     
	public SharedPreferences sp_obj;
	public Editor editor_obj;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.creatingtextnotescreen);


		// accessing shared preference     
		sp_obj=getSharedPreferences("ArchiveApp",MODE_WORLD_WRITEABLE);
		editor_obj=sp_obj.edit(); 
		
		// creating reference for spinner
				sp=(Spinner)findViewById(R.id.spinner_c_txtnotes);
				 
		// fetching user categories 
		ViewingCategoryAndTitleDTO obj_viewcategorytitle =new ViewingCategoryAndTitleDTO("sharan","nonote");
		// assigning the object values, object names and method name
		WSConnectionStrings.parameterobjectvalue=obj_viewcategorytitle;
		WSConnectionStrings.objectname="obj_viewcategorytitle"; 
		WSConnectionStrings.USER_METHOD_NAME=WSConnectionStrings.Method_viewCategories;
		new MyAsyncTask123().execute();
 
		// creating reference for save button    

		Button save=(Button)findViewById(R.id.btn_textsave);
		save.setOnClickListener(new View.OnClickListener() {
   
			@Override 
			public void onClick(View v) {
				// creating reference for controls
				title=(EditText)findViewById(R.id.et_textag);
				contents=(EditText)findViewById(R.id.et_textnote);
				
				// adding preference variable for checking saving or sharing
				
				editor_obj.putString("checkingSAVEorFB", "SAVE");
				editor_obj.commit();
				
				FileInfoStoringDTO obj_fileinfosaving=new FileInfoStoringDTO("sharan","text","",sp.getSelectedItem().toString(),title.getEditableText().toString(),contents.getEditableText().toString());
                
				// assigning the object values, object names and method name
				
				WSConnectionStrings.parameterobjectvalue=obj_fileinfosaving;
				WSConnectionStrings.objectname="obj_fileinfosaving";
				WSConnectionStrings.USER_METHOD_NAME=WSConnectionStrings.METHOD_FILESAVING;
				
				// calling asyntask for network connection
				new MyAsyncTask123().execute();
			}     
		});

		// SHARE Button reference i.e, sharing note on face book

		Button share=(Button)findViewById(R.id.btn_textshare);
		share.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) { 
				// creating reference for controls 
				title=(EditText)findViewById(R.id.et_textag);
				contents=(EditText)findViewById(R.id.et_textnote);

				String usermessage=title.getEditableText().toString()+" \n "+contents.getEditableText().toString();
				
				// adding preference variable for checking saving or sharing
				
				editor_obj.putString("checkingSAVEorFB", "FB");
				editor_obj.putString("check_FBnotestype", "text");
				editor_obj.commit();
				
				// FB LOGIC
				
				Intent intent = new Intent(CreatingTextNotes.this,FBAppActivity.class);
			    intent.putExtra("facebookMessage", usermessage);
			    startActivity(intent);
                 
			}
		});

	}  
	// inner class  For Network Connection i.e, for Web service connection
	class MyAsyncTask123 
	extends AsyncTask<Void, String, String>
	{
		@Override
		protected void onPostExecute(String response) 
		{
              // If response comes while viewing category
			if(WSConnectionStrings.objectname.equals("obj_viewcategorytitle")) 
			{

				categorylist=UserXMLParsingFile.responseOperation(CreatingTextNotes.this,response, "", "categoryname");
				Log.i("categorylist", ""+categorylist);

				// adding array to spinner  
				sp.setAdapter(new ArrayAdapter(CreatingTextNotes.this, android.R.layout.simple_list_item_1, categorylist));
				sp.setOnItemSelectedListener(CreatingTextNotes.this);
				Log.i("categorylistcheck", ""+categorylist); 
			}     
			// If response comes while saving a note
			else if(WSConnectionStrings.objectname.equals("obj_fileinfosaving"))  
			{
				Log.i("save text notes response", ""+response); 
				String[] res=UserXMLParsingFile.responseOperation(CreatingTextNotes.this,response, "", "");
				// not doing any thing as alert is already displayed
			}            
			// if response comes while creating a category
			else if(WSConnectionStrings.objectname.equals("obj_createcategory"))
			{
				 String[] res=UserXMLParsingFile.responseOperation(CreatingTextNotes.this,response, "", "");
			     if(res[0].equals("created category"))
			     {
			    	 ViewingCategoryAndTitleDTO obj_viewcategorytitle =new ViewingCategoryAndTitleDTO("sharan","nonote");
			    	// assigning the object values, object names and method name
			 		WSConnectionStrings.parameterobjectvalue=obj_viewcategorytitle;
			 		WSConnectionStrings.objectname="obj_viewcategorytitle"; 
			 		WSConnectionStrings.USER_METHOD_NAME=WSConnectionStrings.Method_viewCategories;
			 		new MyAsyncTask123().execute();
			     }
			}	
		}
		@Override 
		protected String doInBackground(Void... params) {
			// your network operation
			String wsresponse=WSConnectionFile.wsConn(WSConnectionStrings.parameterobjectvalue, WSConnectionStrings.objectname, 
					WSConnectionStrings.USER_METHOD_NAME,
					WSConnectionStrings.NAMESPACE+WSConnectionStrings.USER_METHOD_NAME);
			return wsresponse; 
		}        
	}
	@Override
	public void onItemSelected(AdapterView<?> arg0, View v, int position,
			long arg3) 
	{
		if(position==0)
		{
			final EditText et=new EditText(v.getContext());
			AlertDialog.Builder alert=new AlertDialog.Builder(v.getContext());
			alert.setTitle("Create a new category");
			alert.setMessage("Enter the category name");
			alert.setView(et);

			alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub

				}   
			});
			alert.setPositiveButton("Create", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Log.i("giventext", et.getEditableText().toString());
					CreateCategoryDTO obj_createcategory=new CreateCategoryDTO("sharan",et.getEditableText().toString()); 
					
					// assigning the object values, object names and method name
					WSConnectionStrings.parameterobjectvalue=obj_createcategory;
					WSConnectionStrings.objectname="obj_createcategory";
					WSConnectionStrings.USER_METHOD_NAME=WSConnectionStrings.METHOD_createUserCategories;
					new MyAsyncTask123().execute();
				}
			});
			alert.show();
		} 
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

} 
